package ase;

import java.sql.SQLException;

import dataConnector.Connector;
import dataDAOInterfaces.DALException;
import dataDAOInterfaces.ICommodityBatchDAO;
import dataDAOInterfaces.ICommodityDAO;
import dataDAOInterfaces.IOperatorDAO;
import dataDAOInterfaces.IProductBatchCompDAO;
import dataDAOInterfaces.IProductBatchDAO;
import dataDAOInterfaces.IRecipeCompDAO;
import dataDAOInterfaces.IRecipeDAO;
import dataDTO.CommodityBatchDTO;
import dataDTO.ProductBatchCompDTO;
import dataDTO.ProductBatchDTO;
import dataDTO.RecipeCompDTO;

public class ASEGets {

	private ICommodityDAO comData;
	private ICommodityBatchDAO comBatchData;
	private IOperatorDAO oprData;
	private IProductBatchDAO prBatchData;
	private IProductBatchCompDAO prBatchCompData;
	private IRecipeDAO recipeData;
	private IRecipeCompDAO recipeComp;
	RecipeCompDTO activeCommodity;
	CommodityBatchDTO comBatchList;

	public ASEGets(ICommodityDAO comData, ICommodityBatchDAO comBatchData, IOperatorDAO oprData, IProductBatchDAO prBatchData, IProductBatchCompDAO prBatchCompData, IRecipeDAO recipeData, IRecipeCompDAO recipeComp){

		this.oprData = oprData;
		this.prBatchCompData=prBatchCompData;
		this.prBatchData=prBatchData;
		this.comData=comData;
		this.comBatchData=comBatchData;
		this.recipeData=recipeData;
		this.recipeComp=recipeComp;

		try {
			Connector conn = new Connector();
			System.out.println(conn);;
		} catch (InstantiationException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		} catch (IllegalAccessException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		} catch (ClassNotFoundException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		} catch (SQLException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}

	}

	public String getName(int oprId) throws DALException{
		String name = oprData.getOperator(oprId).getOprName();
		return name;
	}

	public boolean confirmPb(int prodBatchNo) throws DALException{
		boolean confirmPb = prBatchData.getProductBatchList().contains(prBatchData.getProductBatch(prodBatchNo));
		return confirmPb;
	}

	public int status(int prodBatchNo) throws DALException{
		int status = prBatchData.getProductBatch(prodBatchNo).getStatus();
		return status;
	}

	public boolean productBatchCompEmpty(int prodBatchNo) throws DALException{
		boolean empty = prBatchCompData.getProductBatchCompList(prodBatchNo).isEmpty();
		return empty;
	}

	public int prodBatchCompListSize(int prodBatchNo) throws DALException{
		int size = prBatchCompData.getProductBatchCompList(prodBatchNo).size();
		return size;
	}

	public int getRecipeId(int prodBatchNo) throws DALException{
		int recipeId = prBatchData.getProductBatch(prodBatchNo).getRecipeId();
		return recipeId;
	}

	public String getRecipeName(int recipeId) throws DALException{
		String recipeName = recipeData.getRecipe(recipeId).getRecipeName(); 
		return recipeName;
	}

	public void getRecipeList(int recipeId, int commodityNo) throws DALException{
		activeCommodity = recipeComp.getRecipeCompList(recipeId).get(commodityNo);		
	}

	public int activeCommodityId(){ // gets active commodity Id
		int activeCommodityId = activeCommodity.getCommodityId();
		return activeCommodityId;
	}

	public double activeTolorance(){ // gets active commodity tolerance
		double activeTolorance = activeCommodity.getTolorance();
		return activeTolorance;
	}

	public double activeCommodityNom(){ // gets the nominal net weight of the active commodity
		double activeCommodityNom = activeCommodity.getNomNetto()/1000;
		return activeCommodityNom;
	}

	public String activeCommodityName() throws DALException{  //Finds name of the active commodity
		String activeCommodityName = comData.getCommodity(activeCommodity.getCommodityId()).getCommodityName();
		return activeCommodityName;
	}

	public boolean commodityBatchListEmpty(int activeCommodityId) throws DALException{
		boolean empty = comBatchData.getCommodityBatchList(activeCommodityId).isEmpty();
		return empty;
	}

	public void commodityBatchList(int activeCommodityId, int index) throws DALException{
		comBatchList = comBatchData.getCommodityBatchList(activeCommodityId).get(index);
	}

	public double getBatchVolume(){
		double batchVolume = comBatchList.getVolume();
		return batchVolume;
	}

	public int batchListSize() throws DALException{
		int size = comBatchData.getCommodityBatchList().size();
		return size;
	}

	public int getCommodityBatchId(){
		int comBatchNo = comBatchList.getCbId(); //Gets the Id of the commodity batch
		return comBatchNo;
	}

	public int getRecipeCompSize(int recipeId) throws DALException{
		int size = recipeComp.getRecipeCompList(recipeId).size();
		return size;
	}

	public boolean containsProduct(int prodBatchNo, int comBatchNo) throws DALException{
		boolean contains = prBatchData.getProductBatchList().contains(prBatchCompData.getProductBatchComp(prodBatchNo, comBatchNo));
		return contains;
	}

	public void updateProductBatchComp(int prodBatchNo, int comBatchNo, double taraWeight, double currentWeight, int oprId) throws DALException{
		prBatchCompData.updateProductBatchComp(new ProductBatchCompDTO(prodBatchNo, comBatchNo, taraWeight, currentWeight, oprId));
	}

	public void createProductBatchComp(int prodBatchNo, int comBatchNo, double taraWeight, double currentWeight, int oprId) throws DALException{
		prBatchCompData.createProductBatchComp(new ProductBatchCompDTO(prodBatchNo, comBatchNo, taraWeight, currentWeight, oprId));
	}

	public void updateProductBatch(int prodBatchNo, int status, int recipeId) throws DALException{  //Update product batch status
		prBatchData.updateProductBatch(new ProductBatchDTO(prodBatchNo, status, recipeId));
	}

	public void updateCommodityBatch(int comBatchNo, int activeCommodityId, double currentWeight) throws DALException{
		comBatchData.updateCommodityBatch(new CommodityBatchDTO(comBatchNo, activeCommodityId, getBatchVolume()-currentWeight)); //Update the commodity batch volume
	}
}

